package com.moranyunxi.www.util;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromeUtil {
    public static String getPageSource(String url) {
        // html 页面文本
        String pageSource = "";
        // 设置chromedriver路径
        System.setProperty("webdriver.chrome.driver", "/envConfig/chromedriver");
        //创建一个参数对象，用来控制chrome以无界面模式打开
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--disable-gpu");
        options.addArguments("--no-sandbox");

        ChromeDriver driver = null;
        try {
            driver = new ChromeDriver(options);
            driver.get(url);
            pageSource = driver.getPageSource();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (driver != null) {
                driver.quit();
            }
        }

        return pageSource;
    }
}
